﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BarcodeHandlerForms
{
    public partial class SettingsForm : Form
    {

        public SettingsForm()
        {
            InitializeComponent();

            Configuration config = Utils.GetConfiguration();
            if (config != null)
            {
                this.txtReadPath.Text = string.IsNullOrWhiteSpace(Utils.GetConfiguration().AppSettings.Settings["ReadWriteDirectory"].Value) ? string.Format("{0}ReadWriteDirectory", AppDomain.CurrentDomain.BaseDirectory) : Utils.GetConfiguration().AppSettings.Settings["ReadWriteDirectory"].Value;
                this.txtBackupPath.Text = string.IsNullOrWhiteSpace(Utils.GetConfiguration().AppSettings.Settings["BackupDirectory"].Value) ? string.Format("{0}BackupDirectory", AppDomain.CurrentDomain.BaseDirectory) : Utils.GetConfiguration().AppSettings.Settings["BackupDirectory"].Value;
                this.txtRequestUrl.Text = string.IsNullOrWhiteSpace(Utils.GetConfiguration().AppSettings.Settings["RequestUrl"].Value) ? "http://api.szmd.ltd:52393/Service/wsWIP.asmx/GetEtResult" : Utils.GetConfiguration().AppSettings.Settings["RequestUrl"].Value;
                this.ckbRequest.Checked = bool.Parse(config.AppSettings.Settings["isRequest"].Value);
                this.ckbBackup.Checked = bool.Parse(config.AppSettings.Settings["isBackup"].Value);
                this.ckbAlwaysWrite.Checked = bool.Parse(config.AppSettings.Settings["isAlwaysWrite"].Value);
            }
        }

        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            Configuration config = Utils.GetConfiguration();
            if (config != null)
            {
                config.AppSettings.Settings["ReadWriteDirectory"].Value = this.txtReadPath.Text;
                config.AppSettings.Settings["BackupDirectory"].Value = this.txtBackupPath.Text;
                config.AppSettings.Settings["RequestUrl"].Value = this.txtRequestUrl.Text;
                config.AppSettings.Settings["isRequest"].Value = this.ckbRequest.Checked.ToString();
                config.AppSettings.Settings["isBackup"].Value = this.ckbBackup.Checked.ToString();
                config.AppSettings.Settings["isAlwaysWrite"].Value = this.ckbAlwaysWrite.Checked.ToString();

                BarcodeMainForm.ReadWriteDirectoryPath = this.txtReadPath.Text;
                BarcodeMainForm.BackupDirectoryPath = this.txtBackupPath.Text;
                BarcodeMainForm.SendUrl = this.txtRequestUrl.Text;
                BarcodeMainForm.IsRequest = this.ckbRequest.Checked;
                BarcodeMainForm.IsBackup = this.ckbBackup.Checked;
                BarcodeMainForm.IsAlwaysWrite = this.ckbAlwaysWrite.Checked;
            }
            config.Save(ConfigurationSaveMode.Modified);
            MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            this.Close();
        }

        /// <summary>
        /// 取消设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRead_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "请选择读取文件夹";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtReadPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        /// <summary>
        /// 备份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackup_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "请选择备份文件夹";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtBackupPath.Text = folderBrowserDialog.SelectedPath;
            }
        }
    }
}
